package com.mall.utils;


import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.ObjectMetadata;

import javax.servlet.http.Part;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;


public class UploadUtil {

    public static String updateFile(String path,String name) throws FileNotFoundException {
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        String accessKeyId = "LTAI5tKmdfQZdcThhcW3nhcZ";
        String accessKeySecret = "BLtYu2t8YPQUzW5Kk3v3csiyzJaJst";
        String bucketName = "xymall-wxd";
        //哪个文件夹写在这
        String objectName = "images/"+name;

        ObjectMetadata metadata = new ObjectMetadata();
        //设置http头
        metadata.setContentType("image/jpg");
        //设置文件流
        FileInputStream inputStream = new FileInputStream(path+name);
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {
            ossClient.putObject(bucketName, objectName, inputStream,metadata);
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }

        return "https://xymall-wxd.oss-cn-hangzhou.aliyuncs.com/images/" + name;
    }
    public static String updateFileOnLine(Part image) throws IOException {
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        String accessKeyId = "LTAI5tKmdfQZdcThhcW3nhcZ";
        String accessKeySecret = "BLtYu2t8YPQUzW5Kk3v3csiyzJaJst";
        String bucketName = "xymall-wxd";
        String fileName = image.getSubmittedFileName();
        //随机生成字符串
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        //获取后缀
        String fileNameSuffix =fileName.substring(fileName.lastIndexOf("."));
        String newFileName = uuid+fileNameSuffix;
        //哪个文件夹写在这
        String objectName = "images/"+newFileName;

        ObjectMetadata metadata = new ObjectMetadata();
        //设置http头
        metadata.setContentType("image/jpg");
        //设置文件流
        InputStream inputStream = image.getInputStream();
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {
            ossClient.putObject(bucketName, objectName, inputStream,metadata);
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }

        return "https://xymall-wxd.oss-cn-hangzhou.aliyuncs.com/images/" + newFileName;
    }
}















